import java.util.HashMap;
import java.util.Map;

/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2023-01-08 13:38
 * @Version 1.0
 **/
public class offer_twentytwo1_MapSum {
    public static class Node1 {
        public int pass;
        public int end;
        public Node1[] nexts;

        public Node1() {
            pass = 0;
            end = 0;
            nexts = new Node1[26];
        }
    }

    public static class Triel {
        private Node1 root;

        public Triel() {
            root = new Node1();
        }

        public void insert(String word) {
            if (word == null) {
                return;
            }
            char[] str = word.toCharArray(); // 将字符串转化为字符数组的形式
            Node1 node = root;
            node.pass++;
            int path = 0;
            for (int i = 0; i < str.length; i++) {
                path = str[i] - 'a';
                if (node.nexts[path] == null) {
                    node.nexts[path] = new Node1();
                }
                node = node.nexts[path];
                node.pass++;
            }
            node.end++;
        }

        public void delete(String word) {
            if (search(word) != 0) {
                char[] str = word.toCharArray();
                Node1 node = root;
                node.pass--;
                int index = 0;
                for (int i = 0; i < str.length; i++) {
                    index = str[i] - 'a';
                    if (--node.nexts[index].pass == 0) {
                        node.nexts[index] = null;
                        return;
                    }
                    node = node.nexts[index];
                }
                node.end--;
            }
        }

        //word这个单词之前加入过几次
        public int search(String word) {
            if (word == null) {
                return 0;
            }
            char[] str = word.toCharArray();
            Node1 node = root;
            int path = 0;
            for (int i = 0; i < str.length; i++) {
                path = str[i] - 'a';
                if (node.nexts[path] == null) {
                    return 0;
                }
                node = node.nexts[path];
            }
            return node.end;
        }

        //所有加入的字符串中，有几个是以pre这个字符串作为开始的
        public int prefixNumber(String pre) {
            if (pre == null) {
                return 0;
            }
            char[] str = pre.toCharArray();
            Node1 node = root;
            int index = 0;
            for (int i = 0; i < str.length; i++) {
                index = str[i] - 'a';
                if (node.nexts[index] == null) {
                    return 0;
                }
                node = node.nexts[index];
            }
            return node.pass;
        }

    }

    static class MapSum1 {
        Map<String, Integer> map;

        public MapSum1() {
            map = new HashMap<>();
        }

        public void insert(String key, int val) {
            map.put(key, val);
        }

        public int sum(String prefix) {
            int ans = 0;
            for (String s : map.keySet()) {
                if (s.indexOf(prefix) == 0) {
                    ans += map.get(s);
                }
            }
            return ans;
        }
    }


    class MapSum {
        Node root;
        int sum;
        public MapSum() {
            root = new Node();
        }

        public void insert(String key, int val) {
            Node cur = root;
            for (char ch : key.toCharArray()) {
                int index = ch - 'a';
                if (cur.next[index] == null) {
                    cur.next[index] = new Node();
                }
                cur = cur.next[index];
            }
            cur.isEnd = true;
            cur.val = val;
        }

        public int sum(String prefix) {
            sum = 0;
            Node cur = root;
            for (char ch : prefix.toCharArray()) {
                int index = ch - 'a';
                if (cur.next[index] == null) {
                    return sum;
                }
                cur = cur.next[index];
            }
            find(cur);
            return sum;
        }

        private void find(Node cur) {
            if (cur.isEnd == true) {
                sum += cur.val;
            }
            for (int i = 0; i < 26; i++) {
                if (cur.next[i] != null) {
                    find(cur.next[i]);
                }
            }
            return;
        }

        class Node {
            boolean isEnd;
            int val;
            Node[] next = new Node[26];
        }


    }


}
